package com.alianzamedica.models;

import java.sql.Connection;
import java.sql.PreparedStatement;

import com.alianzamedica.connection.ConnectionImpl;

/**
 * @author Carlos
 * 
 */
public class StockModel {

	/**
	 * actualiza el stock y regresa la respuesta de actualizacion.
	 * 
	 * @param imei
	 * @param numProduit
	 * @param stock
	 * @return respuesta de que el stock se actualizo correctamente.
	 * @throws Exception
	 */
	public boolean updateStock(String imei, int numProduit, int stock)
			throws Exception {
		boolean update = false;
		String sql = "update audit_stock_machine " + " set Stock_LDP = ?  "
				+ "  " + " where Num_machine =(SELECT Num_machine  "
				+ " FROM machine where  Machine_dispo = "
				+ " (SELECT num_modem FROM modem where  Num_carte_sim = ? ) )"
				+ " and Num_produit = ? ";
		PreparedStatement psmt = null;

		ConnectionImpl impl = new ConnectionImpl();
		try {
			impl.open();
			Connection con = impl.getConnection();
			psmt = con.prepareStatement(sql);
			psmt.setInt(1, stock);
			psmt.setString(2, imei);
			psmt.setInt(3, numProduit);
			int iUpdate = psmt.executeUpdate();
			if (iUpdate > 0) {
				update = true;
			}
		} finally {
			if (psmt != null) {
				try {
					psmt.close();
				} catch (Exception e2) {
				}
			}
			impl.close();
		}
		return update;
	}

}
